package com.whzykj.model;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.whzykj.jfinal.TableBind;

@TableBind(name="userinfo",pk="userid")
public class Userinfo extends Model<Userinfo>{
	private static final long serialVersionUID = 1L;
	public static final Userinfo dao=new Userinfo();
	
	/****
	 * 查询用户信息
	 * @param userid
	 * @return
	 */
	public Record getUserInfo(String userid)
	{
//		String sql=" SELECT c.CUST_NAME,c.MOBILE,c.MARKET_ADDRESS,c.STATION_ID,s.station_name FROM userinfo u,customer c,station s WHERE u.CUST_ID=c.CUST_ID AND c.`STATION_ID`=s.`STATION_ID` AND u.`USERID`=? ";
		String sql=" SELECT c.CUST_NAME,c.MOBILE,s.STATION_ADDRESS as MARKET_ADDRESS,c.STATION_ID,s.station_name FROM userinfo u,customer c,station s WHERE u.CUST_ID=c.CUST_ID AND c.`STATION_ID`=s.`STATION_ID` AND u.`USERID`=?";
		return Db.findFirst(sql,userid);
	}
	
	/****
	 * 查询用户信息
	 * @param userid
	 * @return
	 */
	public Record getUserStationById(String stationid)
	{
//		String sql=" SELECT c.CUST_NAME,c.MOBILE,s.station_name,s.img_url FROM userinfo u,customer c,station s WHERE u.CUST_ID=c.CUST_ID AND c.`STATION_ID`=s.`STATION_ID` AND s.station_id=? ";
		String sql=" select ui.USERNAME as CUST_NAME,ui.MOBILE,sta.STATION_NAME,sta.IMG_URL from station sta INNER JOIN userinfo ui on sta.STATION_MASTER=ui.USERID where sta.STATION_ID=?";
//		String sql=" SELECT c.CUST_NAME,c.MOBILE,s.station_name,s.img_url,s.station_address FROM userinfo u,customer c,station s WHERE u.CUST_ID=c.CUST_ID AND c.`STATION_ID`=s.`STATION_ID` AND s.station_id=? ";
		return Db.findFirst(sql,stationid);
	}
	
	public Record userInfo(String userid) {
		String sql =" SELECT u.userid,u.open_id,c.STATION_ID FROM userinfo u,customer c,station s WHERE u.CUST_ID=c.CUST_ID AND c.STATION_ID=s.STATION_ID AND u.USERID=? ";
		return Db.findFirst(sql, userid);
	}
	
	public Record userInfoByuserid(String userid) {
		String sql =" SELECT u.userid,u.cust_id FROM userinfo u,customer c WHERE u.CUST_ID=c.CUST_ID AND u.USERID=? ";
		return Db.findFirst(sql, userid);
	}
	
	/**
	 * 查找指定的电话号码有没有被使用，有则不能注册
	 * @param mobile
	 * @return
	 */
	public boolean findExistsUser(String mobile){
		boolean res=false;
		String sql="select * from userinfo WHERE MOBILE=?";
		Userinfo userinfo=Userinfo.dao.findFirst(sql,mobile);
		if(userinfo==null){
			res=false;
		}else{
			res=true;
		}
		return res;
		
	}
}
